﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenerSoft.JTGL.CommonFunc
{
    class Log
    {
        /// <summary>
        /// 构造方法，写入文件日志
        /// </summary>
        /// <param name="obj"></param>
        public Log(object obj)
        { }

        /// <summary>
        /// 构造方法指定文件夹，写入日志
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="obj"></param>
        public Log(string fileName, object obj)
        { }


        #region 简单记录-> C:\GSLog\EasyRecord.log
        /// <summary>
        /// 简单记录
        /// </summary>
        /// <param name="txt">日志log</param>
        public static void EasyRecord(string txt)
        {
            string vsPath = @"C:\GSLog\";
            if (!Directory.Exists(vsPath))
                Directory.CreateDirectory(vsPath);
            vsPath = vsPath + "EasyRecord.log";
            StreamWriter sw = new StreamWriter(vsPath, true, Encoding.Default);
            DateTime currentTime = System.DateTime.Now;
            sw.WriteLine(String.Format("{0}  ↓↓↓", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
            sw.WriteLine(txt);
            sw.Close();
        }
        #endregion

        #region 类内多方法记录
        /// <summary>
        /// 类内多方法记录,路径：path/assembly.className/MethodName
        /// </summary>
        /// <param name="path">指定路径：如：c:\\GS_Log</param>
        /// <param name="className">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName</param>
        /// <param name="methodName">System.Reflection.MethodBase.GetCurrentMethod().Name</param>
        /// <param name="obj">日志log</param>
        public static void WriteIn(string path ,string className,string methodName, object obj)
        {
            path = String.Format("{3}\\{0}\\{1}\\{2}\\", className, methodName, DateTime.Now.ToString("yyyy-MM-dd-HH"), path);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            path = String.Format("{0}{1}.log", path, System.Threading.Thread.CurrentThread.ManagedThreadId);
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Append, FileAccess.Write, FileShare.ReadWrite);//文件的读写锁
            //System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
            try
            {
                sw.WriteLine(String.Format("{0}  ↓↓↓", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
                sw.WriteLine(obj);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
        #endregion

        #region 一日一记
        /// <summary>
        /// 一日一录
        /// </summary>
        /// <param name="strFilePath">指定日志存放地址</param>
        /// <param name="txt">日志log</param>
        public static void WriteIn(string strFilePath, object txt)
        {
            //string strFilePath = @"c:\\GS\\HRLog\\";
            if (!Directory.Exists(strFilePath))
                System.IO.Directory.CreateDirectory(strFilePath);

            string curDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            strFilePath += DateTime.Now.ToString("yyyy-MM-dd") + "_logfile.log";

           // System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
            System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append, FileAccess.Write, FileShare.ReadWrite);//文件的读写锁
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
            try
            {
                sw.WriteLine(String.Format("{0}  ↓↓↓", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
                sw.WriteLine(txt);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
        #endregion

    }
}
